/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2024, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package com.xjrsoft.module.bi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.module.bi.common.constant.LauncherConstant;
import com.xjrsoft.module.bi.common.tool.*;
import com.xjrsoft.module.bi.factory.MessageTemplate;
import com.xjrsoft.module.bi.pojo.entity.VisualPushTemplate;
import com.xjrsoft.module.bi.service.IVisualPushTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 可视化消息模版配置模块 控制器
 *
 * @author Blade
 */
@RestController
@AllArgsConstructor
@RequestMapping(LauncherConstant.APPLICATION_VISUAL_NAME + "/push/template")
@Api(value = "可视化消息模版配置模块", description = "消息模版配置表接口")
public class VisualPushTemplateController {

	private IVisualPushTemplateService pushTemplateService;
	private MessageTemplate messageTemplate;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R detail(VisualPushTemplate pushTemplate) {
		VisualPushTemplate detail = pushTemplateService.getOne(Condition.getQueryWrapper(pushTemplate));
		return R.ok(detail);
	}

	/**
	 * 分页 消息模版配置表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R list(VisualPushTemplate pushTemplate, Query query) {
		IPage<VisualPushTemplate> pages = pushTemplateService.page(Condition.getPage(query), Condition.getQueryWrapper(pushTemplate));
		return R.ok(pages);
	}

	/**
	 * 新增 消息模版配置表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增")
	public R save(@Valid @RequestBody VisualPushTemplate pushTemplate) {
		return R.ok(pushTemplateService.submit(pushTemplate));
	}

	/**
	 * 修改 消息模版配置表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改")
	public R update(@Valid @RequestBody VisualPushTemplate pushTemplate) {
		return R.ok(pushTemplateService.submit(pushTemplate));
	}

	/**
	 * 新增或修改 消息模版配置表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改")
	public R submit(@Valid @RequestBody VisualPushTemplate pushTemplate) {
		return R.ok(pushTemplateService.submit(pushTemplate));
	}


	/**
	 * 删除 消息模版配置表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.ok(pushTemplateService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 调试 消息模版
	 */
	@PostMapping("/debug")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "调试消息模版")
	public R debug(@RequestParam String code, String params) {
		messageTemplate.sendMessage(code, JsonUtil.parse(params, Kv.class));
		return R.ok("调试完毕");
	}


}
